package net.babypai.widget;

import net.babypai.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BpToast {

	public static void Toast(Context context, String str, int time) {
		final TextView textControl = new TextView(context);
		textControl.setText(str);
		textControl.setBackgroundResource(R.drawable.bp_toast_bg);
		textControl.setGravity(Gravity.CENTER);
		// textControl.setPadding(5, 5, 5, 5);
		textControl.setTextSize(14);
		textControl.setTextColor(Color.parseColor("#ff585858"));
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		((Activity) context).addContentView(textControl, params);
		textControl.measure(0, 0);
		TranslateAnimation ta = new TranslateAnimation(0, 0,
				-textControl.getMeasuredHeight(), 0);
		ta.setDuration(500);
		AlphaAnimation aa = new AlphaAnimation(0.5f, 1);
		aa.setDuration(500);
		TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0,
				-textControl.getMeasuredHeight());
		ta2.setDuration(500);
		ta2.setStartOffset(1500);
		AlphaAnimation aa2 = new AlphaAnimation(1, 0.5f);
		aa2.setDuration(500);
		aa2.setStartOffset(1500);

		AnimationSet as = new AnimationSet(true);
		as.addAnimation(ta2);
		as.addAnimation(aa2);
		as.addAnimation(ta);
		as.addAnimation(aa);
		as.setFillAfter(true);
		as.setAnimationListener(new Animation.AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				textControl.setVisibility(8);
			}
		});
		textControl.startAnimation(as);
	}

	public static void toast(Context context, String str, int time, int isok) {
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		View view = inflater.inflate(R.layout.toast_info, null);
		TextView txt = (TextView) view.findViewById(R.id.bp_toast_text);
		txt.setText(str);
		ImageView img = (ImageView) view.findViewById(R.id.bp_toast_img);
		if (isok == 0) {
			img.setImageResource(R.drawable.bp_dialog_default_icon);
		} else if (isok == 1) {
			img.setImageResource(R.drawable.bp_dialog_suc_icon);
		} else if (isok == 2) {
			img.setImageResource(R.drawable.bp_dialog_err_icon);
		}

		Toast toast = new Toast(context);
		toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 80);
		toast.setDuration(time);
		toast.setView(view);
		toast.show();
	}

}